import requests
import re
import json

headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "priority": "u=0, i",
    "referer": "https://m.jd.com/",
    "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Microsoft Edge";v="138"',
    "sec-ch-ua-mobile": "?1",
    "sec-ch-ua-platform": '"Android"',
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "same-site",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Mobile Safari/537.36 Edg/138.0.0.0",
}
cookies = {
    "b_webp": "1",
    "b_avif": "1",
    "jcap_dvzw_fp": "_HIVLiZCIEZDIcHwXjPU43al5uvsYIwPfs4O6VE9nqABptbi3ASG0seWjzU_o2F4bpTtKrH1VphdN9RwziykSA==",
    "whwswswws": "",
    "autoOpenApp_downCloseDate_auto": "1748508164359_1800000",
    "autoOpenApp_downCloseDate_autoOpenApp_autoPromptly": "1748570511831_1",
    "__jdv": "122270672%7Cdirect%7C-%7Cnone%7C-%7C1751545604909",
    "__jdu": "1751545604909534518369",
    "TrackID": "1io42MDGljZIxKh9nQPT2pDURlFZggkjYvKS71DQbrlTvaOy5RsJZMvGkwdJYjk69Iudaku891qUXOx38zcS0v4514tMicU7e9U2MUQo_Kcck89R-Zvnp0SET6Py7jWmG",
    "light_key": "AASBKE7rOxgWQziEhC_QY6yavDj_xHhkMKVr8vjDB9dBA0T7H7GnmycRgYbZUdKPMeQWX75-",
    "pinId": "briEibRb-h1YgxkG81uaz7V9-x-f3wj7",
    "pin": "jd_7eac037a858fc",
    "unick": "jd_8js7q5tn2s7n2h",
    "_tp": "aO5XtogPiplRTZPyN6P5Vkd5JC%2FQJe7kEmh0g0oTaNI%3D",
    "_pst": "jd_7eac037a858fc",
    "ipLoc-djd": "19-1607-0-0",
    "shshshfpa": "ab96e4b0-82d6-a907-9206-6c096fe807bb-1751545628",
    "shshshfpx": "ab96e4b0-82d6-a907-9206-6c096fe807bb-1751545628",
    "areaId": "19",
    "mail_times": "2%2C1",
    "PCSYCityID": "CN_440000_440300_0",
    "jsavif": "1",
    "__jda": "95931165.1751545604909534518369.1751545604.1751612356.1752224288.3",
    "__jdc": "95931165",
    "wlfstk_smdl": "rar4lhajsosut2ijpchtgjijafkyl2ic",
    "3AB9D23F7A4B3C9B": "GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBE",
    "wxa_level": "1",
    "retina": "0",
    "cid": "9",
    "jxsid": "17522244011849222518",
    "appCode": "ms0ca95114",
    "webp": "1",
    "visitkey": "5225087356839826126",
    "mba_muid": "1751545604909534518369",
    "cd_eid": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMX7C2NSPQAAAAAC7YKOXOEBJ37QAX",
    "sbx_hot_h": "null",
    "autoOpenApp_downCloseDate__jd_czlogin_openapp_": "1752224419174_1",
    "b_dw": "1912",
    "b_dh": "954",
    "b_dpr": "1",
    "bf_hybrid": "d_-",
    "shshshfpb": "BApXSZuC2-_JAYEqgcJFHsyDPBX7ULrZhBhMVUQdq9xJ1MppMe462",
    "TrackerID": "oAk76mdodKzBwUzsurOeo1UbehD1abdVOCEYZt1g_UG9EboR7O-lCWIynQeXdSgibmi-N-nd5ziXFLef0Emzp8OjTHerZLjkughDpyuVaKHDq262EJYGQFcV7ZEdgIPO",
    "pt_key": "AAJocNsmADCZKChm7vBEQs4QxyZFvoNoZ2_1H3-bWCItrCDSSMziykVYJ4oxftE7FCsQ-YqyerU",
    "pt_pin": "jd_7eac037a858fc",
    "pt_token": "yasozpru",
    "pwdt_id": "jd_7eac037a858fc",
    "sfstoken": "tk01mb08e1cbfa8sMysxK1NmMXZ5/FhE4HYdwBQaoBFI0NlX1WYqcGIj9UXazy9ebxY2FqlPhIhlXgjbkxuX9/kTZMeO",
    "qid_uid": "f6952c8d-4d1d-4b40-952c-25d751dd14e8",
    "qid_fs": "1752226601966",
    "qid_ls": "1752226601966",
    "qid_ts": "1752226601974",
    "qid_vis": "1",
    "qid_sid": "f6952c8d-4d1d-4b40-952c-25d751dd14e8-1",
    "p-request-id": "jd_7eac037a858fc2025071117ViqKhcNtvS",
    "3AB9D23F7A4B3CSS": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMX7DNZYAYAAAAACTRB4MMS6ZXDMAX",
    "_gia_d": "1",
    "qid_seq": "3",
    "qid_evord": "6",
    "wqmnx1": "MDEyNjM2NHNqLzU2Nno1aSAubjZwSzdLIEcgZTBTLzYxMDJZYS00MVJTIyEp",
    "__jdb": "95931165.23.1751545604909534518369|3.1752224288",
    "mba_sid": "17522244014911954953588.14",
    "autoOpenApp_downCloseDate_jd_homePage": "1752226834147_1",
    "sdtoken": "AAbEsBpEIOVjqTAKCQtvQu17S7npf7ONgYx8CrePbaHExpdr6Npo2pdrTaqs3VtP64aSmT5XIZvIzAu7tukAR81Pu1VdArzHZxp_VZ7Wwf5XGjz1iSy4mw",
    "pt_st": "1_pKKchMo0E8P2GWp4tdob2OrqqqSDWLzCaO1dQWTaeM8tOH9UzWG1SwCOriGa6qZoVc6ox-wOeIS7H4ZMCoA-J6a98PxoulQj5wK6ZokIKkLbyW_cWGoF1qyAA8A_4_dWyd1DyyXogyIl9bkeZcsZ8VszFAaTnu0KKKaSc5K_Ux6yWDKcBXNzore9LQWGtLrHvG7UoyVBsZKJCWRw7bB7MbMaTlFluhfNQD4u9jHu",
    "__jd_ref_cls": "MSearch_HistoryRecords",
}
url = "https://so.m.jd.com/ware/search.action"
params = {
    "keyword": "电脑写字板",
    "searchFrom": "home",
    "sf": "14",
    "as": "0",
    "sourceType": "H5_home_page_search",
}
response = requests.get(url, headers=headers, cookies=cookies, params=params)

# 检查请求是否成功
if response.status_code == 200:
    html_content = response.text

    # 使用正则表达式提取_sfpageinit中的内容
    match = re.search(r"_sfpageinit\((.*)\)", html_content)
    
    if match:
        json_data = match.group(1)
        
        # 将提取的数据保存为JSON文件
        with open('output.json', 'w', encoding='utf-8') as f:
            json.dump(json.loads(json_data), f, ensure_ascii=False, indent=4)
        print("数据已成功提取并保存为output.json")
    else:
        print("未找到_sfpageinit中的内容")
else:
    print(f"请求失败，状态码: {response.status_code}")